package com.wsx.sky.dubbo.service.impl;

import com.wsx.sky.dubbo.bean.DubboRequest;
import com.wsx.sky.dubbo.bean.DubboResponse;
import com.wsx.sky.dubbo.dao.DubboServerMapper;
import com.wsx.sky.dubbo.model.DubboServer;
import com.wsx.sky.dubbo.service.DubboClientService;
import com.wsx.sky.dubbo.service.DubboService;
import com.wsx.sky.dubbo.service.HttpServerFeign;
import com.wsx.sky.http.HttpClientUtils;
import com.wsx.sky.http.bean.HttpResponse;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.dubbo.service.impl.DubboServerServiceImpl
 * @Description: TODO
 * @date 2018/12/11
 */
@Component
public class DubboClientServiceImpl implements DubboClientService {

    @Autowired
    private DubboService dubboService;

    @Autowired
    private HttpServerFeign httpServerFeign;

    @Autowired
    private DubboServerMapper dubboServerMapper;

    private static final String GET_URL = "http://127.0.0.1:8132/http/server/get/feign/";

    @Override
    public DubboResponse dubbo(DubboRequest dubboRequest) {
        System.out.println(dubboRequest.getTraceid());
        DubboResponse dubboResponse = check(dubboRequest);
        if (null != dubboResponse) {
            return dubboResponse;
        }
        try {
            DubboServer dubboServer = dubboServerMapper.get(dubboRequest.getName());

            dubboResponse =  dubboService.request(dubboRequest);

            return dubboResponse.buildMsg(dubboResponse.getMsg() + "--" + dubboServer.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new DubboResponse(50001).buildMsg("dubbo client error");
    }

    @Override
    public DubboResponse http(DubboRequest dubboRequest) {
        DubboResponse dubboResponse = check(dubboRequest);
        if (null != dubboResponse) {
            return dubboResponse;
        }
        try {
            String value = HttpClientUtils.get(GET_URL + dubboRequest.getName(), null);
            if (!StringUtils.isEmpty(value)) {
                return new DubboResponse().buildMsg(value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new DubboResponse(50001).buildMsg("dubbo http client error");
    }

    @Override
    public DubboResponse complex(DubboRequest dubboRequest) {
        DubboResponse dubboResponse = check(dubboRequest);
        if (null != dubboResponse) {
            return dubboResponse;
        }

        try {
            dubboResponse = dubboService.request(dubboRequest);

            HttpResponse response = httpServerFeign.get(dubboRequest.getName());

            dubboResponse.setMsg(dubboResponse.getMsg() + "--" + response.getMsg());
            return dubboResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new DubboResponse(50001).buildMsg("dubbo http complex error");
    }

    private DubboResponse check(DubboRequest dubboRequest) {
        if (null == dubboRequest
                || StringUtils.isEmpty(dubboRequest.getName())) {
            return new DubboResponse(30001).buildMsg("dubbo client param error");
        }
        return null;
    }
}
